<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>range_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_editor_range_test.js.html">range_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.editor.rangeTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.editor.rangeTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.Range&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line21"></a>goog.require(&#39;goog.editor.range&#39;);
<a name="line22"></a>goog.require(&#39;goog.editor.range.Point&#39;);
<a name="line23"></a>goog.require(&#39;goog.string&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line26"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line27"></a>
<a name="line28"></a>var savedHtml;
<a name="line29"></a>var $;
<a name="line30"></a>
<a name="line31"></a>function setUpPage() {
<a name="line32"></a>  $ = goog.dom.getElement;
<a name="line33"></a>}
<a name="line34"></a>
<a name="line35"></a>function setUp() {
<a name="line36"></a>  savedHtml = $(&#39;root&#39;).innerHTML;
<a name="line37"></a>}
<a name="line38"></a>
<a name="line39"></a>function tearDown() {
<a name="line40"></a>  $(&#39;root&#39;).innerHTML = savedHtml;
<a name="line41"></a>}
<a name="line42"></a>
<a name="line43"></a>function testNoNarrow() {
<a name="line44"></a>  var def = $(&#39;def&#39;);
<a name="line45"></a>  var jkl = $(&#39;jkl&#39;);
<a name="line46"></a>  var range = goog.dom.Range.createFromNodes(
<a name="line47"></a>      def.firstChild, 1, jkl.firstChild, 2);
<a name="line48"></a>
<a name="line49"></a>  range = goog.editor.range.narrow(range, $(&#39;parentNode&#39;));
<a name="line50"></a>  goog.testing.dom.assertRangeEquals(
<a name="line51"></a>      def.firstChild, 1, jkl.firstChild, 2, range);
<a name="line52"></a>}
<a name="line53"></a>
<a name="line54"></a>function testNarrowAtEndEdge() {
<a name="line55"></a>  var def = $(&#39;def&#39;);
<a name="line56"></a>  var jkl = $(&#39;jkl&#39;);
<a name="line57"></a>  var range = goog.dom.Range.createFromNodes(
<a name="line58"></a>      def.firstChild, 1, jkl.firstChild, 2);
<a name="line59"></a>
<a name="line60"></a>  range = goog.editor.range.narrow(range, def);
<a name="line61"></a>  goog.testing.dom.assertRangeEquals(
<a name="line62"></a>      def.firstChild, 1, def.firstChild, 3, range);
<a name="line63"></a>}
<a name="line64"></a>
<a name="line65"></a>function testNarrowAtStartEdge() {
<a name="line66"></a>  var def = $(&#39;def&#39;);
<a name="line67"></a>  var jkl = $(&#39;jkl&#39;);
<a name="line68"></a>  var range = goog.dom.Range.createFromNodes(
<a name="line69"></a>      def.firstChild, 1, jkl.firstChild, 2);
<a name="line70"></a>
<a name="line71"></a>  range = goog.editor.range.narrow(range, jkl);
<a name="line72"></a>
<a name="line73"></a>  goog.testing.dom.assertRangeEquals(
<a name="line74"></a>      jkl.firstChild, 0, jkl.firstChild, 2, range);
<a name="line75"></a>}
<a name="line76"></a>
<a name="line77"></a>function testNarrowOutsideElement() {
<a name="line78"></a>  var def = $(&#39;def&#39;);
<a name="line79"></a>  var jkl = $(&#39;jkl&#39;);
<a name="line80"></a>  var range = goog.dom.Range.createFromNodes(
<a name="line81"></a>      def.firstChild, 1, jkl.firstChild, 2);
<a name="line82"></a>
<a name="line83"></a>  range = goog.editor.range.narrow(range, $(&#39;pqr&#39;));
<a name="line84"></a>  assertNull(range);
<a name="line85"></a>}
<a name="line86"></a>
<a name="line87"></a>function testNoExpand() {
<a name="line88"></a>  var div = $(&#39;parentNode&#39;);
<a name="line89"></a>  div.innerHTML = &#39;&lt;div&gt;longword&lt;/div&gt;&#39;;
<a name="line90"></a>  // Select &quot;ongwo&quot; and make sure we don&#39;t expand since this is not
<a name="line91"></a>  // a full container.
<a name="line92"></a>  var textNode = div.firstChild.firstChild;
<a name="line93"></a>  var range = goog.dom.Range.createFromNodes(textNode, 1, textNode, 6);
<a name="line94"></a>
<a name="line95"></a>  range = goog.editor.range.expand(range);
<a name="line96"></a>  goog.testing.dom.assertRangeEquals(textNode, 1, textNode, 6, range);
<a name="line97"></a>}
<a name="line98"></a>
<a name="line99"></a>function testSimpleExpand() {
<a name="line100"></a>  var div = $(&#39;parentNode&#39;);
<a name="line101"></a>  div.innerHTML = &#39;&lt;div&gt;longword&lt;/div&gt;foo&#39;;
<a name="line102"></a>  // Select &quot;longword&quot; and make sure we do expand to include the div since
<a name="line103"></a>  // the full container text is selected.
<a name="line104"></a>  var textNode = div.firstChild.firstChild;
<a name="line105"></a>  var range = goog.dom.Range.createFromNodes(textNode, 0, textNode, 8);
<a name="line106"></a>
<a name="line107"></a>  range = goog.editor.range.expand(range);
<a name="line108"></a>  goog.testing.dom.assertRangeEquals(div, 0, div, 1, range);
<a name="line109"></a>
<a name="line110"></a>  // Select &quot;foo&quot; and make sure we expand out to the parent div.
<a name="line111"></a>  var fooNode = div.lastChild;
<a name="line112"></a>  range = goog.dom.Range.createFromNodes(fooNode, 0, fooNode, 3);
<a name="line113"></a>
<a name="line114"></a>  range = goog.editor.range.expand(range);
<a name="line115"></a>  goog.testing.dom.assertRangeEquals(div, 1, div, 2, range);
<a name="line116"></a>}
<a name="line117"></a>
<a name="line118"></a>function testDoubleExpand() {
<a name="line119"></a>  var div = $(&#39;parentNode&#39;);
<a name="line120"></a>  div.innerHTML = &#39;&lt;div&gt;&lt;span&gt;longword&lt;/span&gt;&lt;/div&gt;foo&#39;;
<a name="line121"></a>  // Select &quot;longword&quot; and make sure we do expand to include the span
<a name="line122"></a>  // and the div since both of their full contents are selected.
<a name="line123"></a>  var textNode = div.firstChild.firstChild.firstChild;
<a name="line124"></a>  var range = goog.dom.Range.createFromNodes(textNode, 0, textNode, 8);
<a name="line125"></a>
<a name="line126"></a>  range = goog.editor.range.expand(range);
<a name="line127"></a>  goog.testing.dom.assertRangeEquals(div, 0, div, 1, range);
<a name="line128"></a>
<a name="line129"></a>  // Same visible position, different dom position.
<a name="line130"></a>  // Start in text node, end in span.
<a name="line131"></a>  range = goog.dom.Range.createFromNodes(textNode, 0, textNode.parentNode, 1);
<a name="line132"></a>
<a name="line133"></a>  range = goog.editor.range.expand(range);
<a name="line134"></a>  goog.testing.dom.assertRangeEquals(div, 0, div, 1, range);
<a name="line135"></a>}
<a name="line136"></a>
<a name="line137"></a>function testMultipleChildrenExpand() {
<a name="line138"></a>  var div = $(&#39;parentNode&#39;);
<a name="line139"></a>  div.innerHTML = &#39;&lt;ol&gt;&lt;li&gt;one&lt;/li&gt;&lt;li&gt;two&lt;/li&gt;&lt;li&gt;three&lt;/li&gt;&lt;/ol&gt;&#39;;
<a name="line140"></a>  // Select &quot;two&quot; and make sure we expand to the li, but not the ol.
<a name="line141"></a>  var li = div.firstChild.childNodes[1];
<a name="line142"></a>  var textNode = li.firstChild;
<a name="line143"></a>  var range = goog.dom.Range.createFromNodes(textNode, 0, textNode, 3);
<a name="line144"></a>
<a name="line145"></a>  range = goog.editor.range.expand(range);
<a name="line146"></a>  goog.testing.dom.assertRangeEquals(
<a name="line147"></a>      li.parentNode, 1, li.parentNode, 2, range);
<a name="line148"></a>
<a name="line149"></a>  // Make the same visible selection, only slightly different dom position.
<a name="line150"></a>  // Select starting from the text node, but ending in the li.
<a name="line151"></a>  range = goog.dom.Range.createFromNodes(textNode, 0, li, 1);
<a name="line152"></a>
<a name="line153"></a>  range = goog.editor.range.expand(range);
<a name="line154"></a>  goog.testing.dom.assertRangeEquals(
<a name="line155"></a>      li.parentNode, 1, li.parentNode, 2, range);
<a name="line156"></a>}
<a name="line157"></a>
<a name="line158"></a>function testSimpleDifferentContainersExpand() {
<a name="line159"></a>  var div = $(&#39;parentNode&#39;);
<a name="line160"></a>  div.innerHTML = &#39;&lt;ol&gt;&lt;li&gt;1&lt;/li&gt;&lt;li&gt;&lt;b&gt;bold&lt;/b&gt;&lt;i&gt;italic&lt;/i&gt;&lt;/li&gt;&lt;/ol&gt;&#39;;
<a name="line161"></a>  // Select all of &quot;bold&quot; and &quot;italic&quot; at the text node level, and
<a name="line162"></a>  // make sure we expand to the li.
<a name="line163"></a>  var li = div.firstChild.childNodes[1];
<a name="line164"></a>  var boldNode = li.childNodes[0];
<a name="line165"></a>  var italicNode = li.childNodes[1];
<a name="line166"></a>  var range = goog.dom.Range.createFromNodes(boldNode.firstChild, 0,
<a name="line167"></a>      italicNode.firstChild, 6);
<a name="line168"></a>
<a name="line169"></a>  range = goog.editor.range.expand(range);
<a name="line170"></a>  goog.testing.dom.assertRangeEquals(
<a name="line171"></a>      li.parentNode, 1, li.parentNode, 2, range);
<a name="line172"></a>
<a name="line173"></a>  // Make the same visible selection, only slightly different dom position.
<a name="line174"></a>  // Select &quot;bold&quot; at the b node level and &quot;italic&quot; at the text node level.
<a name="line175"></a>  range = goog.dom.Range.createFromNodes(boldNode, 0,
<a name="line176"></a>      italicNode.firstChild, 6);
<a name="line177"></a>
<a name="line178"></a>  range = goog.editor.range.expand(range);
<a name="line179"></a>  goog.testing.dom.assertRangeEquals(
<a name="line180"></a>      li.parentNode, 1, li.parentNode, 2, range);
<a name="line181"></a>}
<a name="line182"></a>
<a name="line183"></a>function testSimpleDifferentContainersSmallExpand() {
<a name="line184"></a>  var div = $(&#39;parentNode&#39;);
<a name="line185"></a>  div.innerHTML = &#39;&lt;ol&gt;&lt;li&gt;1&lt;/li&gt;&lt;li&gt;&lt;b&gt;bold&lt;/b&gt;&lt;i&gt;italic&lt;/i&gt;&#39; +
<a name="line186"></a>      &#39;&lt;u&gt;under&lt;/u&gt;&lt;/li&gt;&lt;/ol&gt;&#39;;
<a name="line187"></a>  // Select all of &quot;bold&quot; and &quot;italic&quot;, but we can&#39;t expand to the
<a name="line188"></a>  // entire li since we didn&#39;t select &quot;under&quot;.
<a name="line189"></a>  var li = div.firstChild.childNodes[1];
<a name="line190"></a>  var boldNode = li.childNodes[0];
<a name="line191"></a>  var italicNode = li.childNodes[1];
<a name="line192"></a>  var range = goog.dom.Range.createFromNodes(boldNode.firstChild, 0,
<a name="line193"></a>      italicNode.firstChild, 6);
<a name="line194"></a>
<a name="line195"></a>  range = goog.editor.range.expand(range);
<a name="line196"></a>  goog.testing.dom.assertRangeEquals(li, 0, li, 2, range);
<a name="line197"></a>
<a name="line198"></a>  // Same visible position, different dom position.
<a name="line199"></a>  // Select &quot;bold&quot; starting in text node, &quot;italic&quot; at i node.
<a name="line200"></a>  range = goog.dom.Range.createFromNodes(boldNode.firstChild, 0,
<a name="line201"></a>      italicNode, 1);
<a name="line202"></a>
<a name="line203"></a>  range = goog.editor.range.expand(range);
<a name="line204"></a>  goog.testing.dom.assertRangeEquals(li, 0, li, 2, range);
<a name="line205"></a>}
<a name="line206"></a>
<a name="line207"></a>function testEmbeddedDifferentContainersExpand() {
<a name="line208"></a>  var div = $(&#39;parentNode&#39;);
<a name="line209"></a>  div.innerHTML = &#39;&lt;div&gt;&lt;b&gt;&lt;i&gt;italic&lt;/i&gt;after&lt;/b&gt;&lt;u&gt;under&lt;/u&gt;&lt;/div&gt;foo&#39;;
<a name="line210"></a>  // Select &quot;italic&quot; &quot;after&quot; &quot;under&quot;, should expand all the way to parent.
<a name="line211"></a>  var boldNode = div.firstChild.childNodes[0];
<a name="line212"></a>  var italicNode = boldNode.childNodes[0];
<a name="line213"></a>  var underNode = div.firstChild.childNodes[1];
<a name="line214"></a>  var range = goog.dom.Range.createFromNodes(italicNode.firstChild, 0,
<a name="line215"></a>      underNode.firstChild, 5);
<a name="line216"></a>
<a name="line217"></a>  range = goog.editor.range.expand(range);
<a name="line218"></a>  goog.testing.dom.assertRangeEquals(div, 0, div, 1, range);
<a name="line219"></a>}
<a name="line220"></a>
<a name="line221"></a>function testReverseSimpleExpand() {
<a name="line222"></a>  var div = $(&#39;parentNode&#39;);
<a name="line223"></a>  div.innerHTML = &#39;&lt;div&gt;longword&lt;/div&gt;foo&#39;;
<a name="line224"></a>  // Select &quot;longword&quot; and make sure we do expand to include the div since
<a name="line225"></a>  // the full container text is selected.
<a name="line226"></a>  var textNode = div.firstChild.firstChild;
<a name="line227"></a>  var range = goog.dom.Range.createFromNodes(textNode, 8, textNode, 0);
<a name="line228"></a>
<a name="line229"></a>  range = goog.editor.range.expand(range);
<a name="line230"></a>
<a name="line231"></a>  goog.testing.dom.assertRangeEquals(div, 0, div, 1, range);
<a name="line232"></a>}
<a name="line233"></a>
<a name="line234"></a>function testExpandWithStopNode() {
<a name="line235"></a>  var div = $(&#39;parentNode&#39;);
<a name="line236"></a>  div.innerHTML = &#39;&lt;div&gt;&lt;span&gt;word&lt;/span&gt;&lt;/div&gt;foo&#39;;
<a name="line237"></a>  // Select &quot;word&quot;.
<a name="line238"></a>  var span = div.firstChild.firstChild;
<a name="line239"></a>  var textNode = span.firstChild;
<a name="line240"></a>  var range = goog.dom.Range.createFromNodes(textNode, 0, textNode, 4);
<a name="line241"></a>
<a name="line242"></a>  range = goog.editor.range.expand(range);
<a name="line243"></a>
<a name="line244"></a>  goog.testing.dom.assertRangeEquals(div, 0, div, 1, range);
<a name="line245"></a>
<a name="line246"></a>  // Same selection, but force stop at the span.
<a name="line247"></a>  range = goog.dom.Range.createFromNodes(textNode, 0, textNode, 4);
<a name="line248"></a>
<a name="line249"></a>  range = goog.editor.range.expand(range, span);
<a name="line250"></a>
<a name="line251"></a>  goog.testing.dom.assertRangeEquals(span, 0, span, 1, range);
<a name="line252"></a>}
<a name="line253"></a>
<a name="line254"></a>// Ojan didn&#39;t believe this code worked, this was the case he
<a name="line255"></a>// thought was broken.  Keeping just as a regression test.
<a name="line256"></a>function testOjanCase() {
<a name="line257"></a>  var div = $(&#39;parentNode&#39;);
<a name="line258"></a>  div.innerHTML = &#39;&lt;em&gt;&lt;i&gt;&lt;b&gt;foo&lt;/b&gt;bar&lt;/i&gt;&lt;/em&gt;&#39;;
<a name="line259"></a>  // Select &quot;foo&quot;, at text node level.
<a name="line260"></a>  var iNode = div.firstChild.firstChild;
<a name="line261"></a>  var textNode = iNode.firstChild.firstChild;
<a name="line262"></a>  var range = goog.dom.Range.createFromNodes(textNode, 0, textNode, 3);
<a name="line263"></a>
<a name="line264"></a>  range = goog.editor.range.expand(range);
<a name="line265"></a>
<a name="line266"></a>  goog.testing.dom.assertRangeEquals(iNode, 0, iNode, 1, range);
<a name="line267"></a>
<a name="line268"></a>  // Same selection, at b node level.
<a name="line269"></a>  range = goog.dom.Range.createFromNodes(iNode.firstChild, 0,
<a name="line270"></a>      iNode.firstChild, 1);
<a name="line271"></a>  range = goog.editor.range.expand(range);
<a name="line272"></a>
<a name="line273"></a>  goog.testing.dom.assertRangeEquals(iNode, 0, iNode, 1, range);
<a name="line274"></a>}
<a name="line275"></a>
<a name="line276"></a>function testPlaceCursorNextToLeft() {
<a name="line277"></a>  var div = $(&#39;parentNode&#39;);
<a name="line278"></a>  div.innerHTML = &#39;foo&lt;div id=&quot;bar&quot;&gt;bar&lt;/div&gt;baz&#39;;
<a name="line279"></a>  var node = $(&#39;bar&#39;);
<a name="line280"></a>  var range = goog.editor.range.placeCursorNextTo(node, true);
<a name="line281"></a>
<a name="line282"></a>  var expose = goog.testing.dom.exposeNode;
<a name="line283"></a>  assertEquals(&#39;Selection should be to the left of the node &#39; +
<a name="line284"></a>      expose(node) + &#39;,&#39; + expose(range.getStartNode().nextSibling),
<a name="line285"></a>      node, range.getStartNode().nextSibling);
<a name="line286"></a>  assertEquals(&#39;Selection should be collapsed&#39;,
<a name="line287"></a>      true, range.isCollapsed());
<a name="line288"></a>}
<a name="line289"></a>
<a name="line290"></a>
<a name="line291"></a>function testPlaceCursorNextToRight() {
<a name="line292"></a>  var div = $(&#39;parentNode&#39;);
<a name="line293"></a>  div.innerHTML = &#39;foo&lt;div id=&quot;bar&quot;&gt;bar&lt;/div&gt;baz&#39;;
<a name="line294"></a>  var node = $(&#39;bar&#39;);
<a name="line295"></a>  var range = goog.editor.range.placeCursorNextTo(node, false);
<a name="line296"></a>
<a name="line297"></a>  assertEquals(&#39;Selection should be to the right of the node&#39;,
<a name="line298"></a>      node, range.getStartNode().previousSibling);
<a name="line299"></a>  assertEquals(&#39;Selection should be collapsed&#39;,
<a name="line300"></a>      true, range.isCollapsed());
<a name="line301"></a>}
<a name="line302"></a>
<a name="line303"></a>function testPlaceCursorNextTo_rightOfLineBreak() {
<a name="line304"></a>  var div = $(&#39;parentNode&#39;);
<a name="line305"></a>  div.innerHTML = &#39;&lt;div contentEditable=&quot;true&quot;&gt;hhhh&lt;br /&gt;h&lt;/div&gt;&#39;;
<a name="line306"></a>  var children = div.firstChild.childNodes;
<a name="line307"></a>  assertEquals(3, children.length);
<a name="line308"></a>  var node = children[1];
<a name="line309"></a>  var range = goog.editor.range.placeCursorNextTo(node, false);
<a name="line310"></a>  assertEquals(node.nextSibling, range.getStartNode());
<a name="line311"></a>}
<a name="line312"></a>
<a name="line313"></a>function testPlaceCursorNextTo_leftOfHr() {
<a name="line314"></a>  var div = $(&#39;parentNode&#39;);
<a name="line315"></a>  div.innerHTML = &#39;&lt;hr /&gt;aaa&#39;;
<a name="line316"></a>  var children = div.childNodes;
<a name="line317"></a>  assertEquals(2, children.length);
<a name="line318"></a>  var node = children[0];
<a name="line319"></a>  var range = goog.editor.range.placeCursorNextTo(node, true);
<a name="line320"></a>
<a name="line321"></a>  assertEquals(div, range.getStartNode());
<a name="line322"></a>  assertEquals(0, range.getStartOffset());
<a name="line323"></a>}
<a name="line324"></a>
<a name="line325"></a>function testPlaceCursorNextTo_rightOfHr() {
<a name="line326"></a>  var div = $(&#39;parentNode&#39;);
<a name="line327"></a>  div.innerHTML = &#39;aaa&lt;hr&gt;&#39;;
<a name="line328"></a>  var children = div.childNodes;
<a name="line329"></a>  assertEquals(2, children.length);
<a name="line330"></a>  var node = children[1];
<a name="line331"></a>  var range = goog.editor.range.placeCursorNextTo(node, false);
<a name="line332"></a>
<a name="line333"></a>  assertEquals(div, range.getStartNode());
<a name="line334"></a>  assertEquals(2, range.getStartOffset());
<a name="line335"></a>}
<a name="line336"></a>
<a name="line337"></a>function testPlaceCursorNextTo_rightOfImg() {
<a name="line338"></a>  var div = $(&#39;parentNode&#39;);
<a name="line339"></a>  div.innerHTML =
<a name="line340"></a>      &#39;aaa&lt;img src=&quot;https://www.google.com/images/srpr/logo3w.png&quot;&gt;bbb&#39;;
<a name="line341"></a>  var children = div.childNodes;
<a name="line342"></a>  assertEquals(3, children.length);
<a name="line343"></a>  var imgNode = children[1];
<a name="line344"></a>  var range = goog.editor.range.placeCursorNextTo(imgNode, false);
<a name="line345"></a>
<a name="line346"></a>  assertEquals(&#39;range node should be the right sibling of img tag&#39;,
<a name="line347"></a>      children[2], range.getStartNode());
<a name="line348"></a>  assertEquals(0, range.getStartOffset());
<a name="line349"></a>
<a name="line350"></a>}
<a name="line351"></a>
<a name="line352"></a>function testPlaceCursorNextTo_rightOfImgAtEnd() {
<a name="line353"></a>  var div = $(&#39;parentNode&#39;);
<a name="line354"></a>  div.innerHTML =
<a name="line355"></a>      &#39;aaa&lt;img src=&quot;https://www.google.com/images/srpr/logo3w.png&quot;&gt;&#39;;
<a name="line356"></a>  var children = div.childNodes;
<a name="line357"></a>  assertEquals(2, children.length);
<a name="line358"></a>  var imgNode = children[1];
<a name="line359"></a>  var range = goog.editor.range.placeCursorNextTo(imgNode, false);
<a name="line360"></a>
<a name="line361"></a>  assertEquals(&#39;range node should be the parent of img&#39;,
<a name="line362"></a>      div, range.getStartNode());
<a name="line363"></a>  assertEquals(&#39;offset should be right after the img tag&#39;,
<a name="line364"></a>      2, range.getStartOffset());
<a name="line365"></a>
<a name="line366"></a>}
<a name="line367"></a>
<a name="line368"></a>function testPlaceCursorNextTo_leftOfImg() {
<a name="line369"></a>  var div = $(&#39;parentNode&#39;);
<a name="line370"></a>  div.innerHTML =
<a name="line371"></a>      &#39;&lt;img src=&quot;https://www.google.com/images/srpr/logo3w.png&quot;&gt;xxx&#39;;
<a name="line372"></a>  var children = div.childNodes;
<a name="line373"></a>  assertEquals(2, children.length);
<a name="line374"></a>  var imgNode = children[0];
<a name="line375"></a>  var range = goog.editor.range.placeCursorNextTo(imgNode, true);
<a name="line376"></a>
<a name="line377"></a>  assertEquals(&#39;range node should be the parent of img&#39;,
<a name="line378"></a>      div, range.getStartNode());
<a name="line379"></a>  assertEquals(&#39;offset should point to the img tag&#39;,
<a name="line380"></a>      0, range.getStartOffset());
<a name="line381"></a>}
<a name="line382"></a>
<a name="line383"></a>function testPlaceCursorNextTo_rightOfFirstOfTwoImgTags() {
<a name="line384"></a>  var div = $(&#39;parentNode&#39;);
<a name="line385"></a>  div.innerHTML =
<a name="line386"></a>      &#39;aaa&lt;img src=&quot;https://www.google.com/images/srpr/logo3w.png&quot;&gt;&#39; +
<a name="line387"></a>      &#39;&lt;img src=&quot;https://www.google.com/images/srpr/logo3w.png&quot;&gt;&#39;;
<a name="line388"></a>  var children = div.childNodes;
<a name="line389"></a>  assertEquals(3, children.length);
<a name="line390"></a>  var imgNode = children[1];  // First of two IMG nodes
<a name="line391"></a>  var range = goog.editor.range.placeCursorNextTo(imgNode, false);
<a name="line392"></a>
<a name="line393"></a>  assertEquals(&#39;range node should be the parent of img instead of &#39; +
<a name="line394"></a>      &#39;node with innerHTML=&#39; + range.getStartNode().innerHTML,
<a name="line395"></a>      div, range.getStartNode());
<a name="line396"></a>  assertEquals(&#39;offset should be right after the img tag&#39;,
<a name="line397"></a>      2, range.getStartOffset());
<a name="line398"></a>}
<a name="line399"></a>
<a name="line400"></a>function testGetDeepEndPoint() {
<a name="line401"></a>  var div = $(&#39;parentNode&#39;);
<a name="line402"></a>  var def = $(&#39;def&#39;);
<a name="line403"></a>  var jkl = $(&#39;jkl&#39;);
<a name="line404"></a>
<a name="line405"></a>  assertPointEquals(div.firstChild, 0,
<a name="line406"></a>      goog.editor.range.getDeepEndPoint(
<a name="line407"></a>          goog.dom.Range.createFromNodeContents(div), true));
<a name="line408"></a>  assertPointEquals(div.lastChild, div.lastChild.length,
<a name="line409"></a>      goog.editor.range.getDeepEndPoint(
<a name="line410"></a>          goog.dom.Range.createFromNodeContents(div), false));
<a name="line411"></a>
<a name="line412"></a>  assertPointEquals(def.firstChild, 0,
<a name="line413"></a>      goog.editor.range.getDeepEndPoint(
<a name="line414"></a>          goog.dom.Range.createCaret(div, 1), true));
<a name="line415"></a>  assertPointEquals(def.nextSibling, 0,
<a name="line416"></a>      goog.editor.range.getDeepEndPoint(
<a name="line417"></a>          goog.dom.Range.createCaret(div, 2), true));
<a name="line418"></a>
<a name="line419"></a>}
<a name="line420"></a>
<a name="line421"></a>function testNormalizeOnNormalizedDom() {
<a name="line422"></a>  var defText = $(&#39;def&#39;).firstChild;
<a name="line423"></a>  var jklText = $(&#39;jkl&#39;).firstChild;
<a name="line424"></a>  var range = goog.dom.Range.createFromNodes(defText, 1, jklText, 2);
<a name="line425"></a>
<a name="line426"></a>  var newRange = normalizeBody(range);
<a name="line427"></a>  goog.testing.dom.assertRangeEquals(defText, 1, jklText, 2, newRange);
<a name="line428"></a>}
<a name="line429"></a>
<a name="line430"></a>function testDeepPointFindingOnNormalizedDom() {
<a name="line431"></a>  var def = $(&#39;def&#39;);
<a name="line432"></a>  var jkl = $(&#39;jkl&#39;);
<a name="line433"></a>  var range = goog.dom.Range.createFromNodes(def, 0, jkl, 1);
<a name="line434"></a>
<a name="line435"></a>  var newRange = normalizeBody(range);
<a name="line436"></a>
<a name="line437"></a>  // Make sure that newRange is measured relative to the text nodes,
<a name="line438"></a>  // not the DIV elements.
<a name="line439"></a>  goog.testing.dom.assertRangeEquals(
<a name="line440"></a>      def.firstChild, 0, jkl.firstChild, 3, newRange);
<a name="line441"></a>}
<a name="line442"></a>
<a name="line443"></a>function testNormalizeOnVeryFragmentedDom() {
<a name="line444"></a>  var defText = $(&#39;def&#39;).firstChild;
<a name="line445"></a>  var jklText = $(&#39;jkl&#39;).firstChild;
<a name="line446"></a>  var range = goog.dom.Range.createFromNodes(defText, 1, jklText, 2);
<a name="line447"></a>
<a name="line448"></a>  // Fragment the DOM a bunch.
<a name="line449"></a>  fragmentText(defText);
<a name="line450"></a>  fragmentText(jklText);
<a name="line451"></a>
<a name="line452"></a>  var newRange = normalizeBody(range);
<a name="line453"></a>
<a name="line454"></a>  // our old text nodes may not be valid anymore. find new ones.
<a name="line455"></a>  defText = $(&#39;def&#39;).firstChild;
<a name="line456"></a>  jklText = $(&#39;jkl&#39;).firstChild;
<a name="line457"></a>
<a name="line458"></a>  goog.testing.dom.assertRangeEquals(defText, 1, jklText, 2, newRange);
<a name="line459"></a>}
<a name="line460"></a>
<a name="line461"></a>function testNormalizeOnDivWithEmptyTextNodes() {
<a name="line462"></a>  var emptyDiv = $(&#39;normalizeTest-with-empty-text-nodes&#39;);
<a name="line463"></a>
<a name="line464"></a>  // Append empty text nodes to the emptyDiv.
<a name="line465"></a>  var tnode1 = goog.dom.createTextNode(&#39;&#39;);
<a name="line466"></a>  var tnode2 = goog.dom.createTextNode(&#39;&#39;);
<a name="line467"></a>  var tnode3 = goog.dom.createTextNode(&#39;&#39;);
<a name="line468"></a>
<a name="line469"></a>  goog.dom.appendChild(emptyDiv, tnode1);
<a name="line470"></a>  goog.dom.appendChild(emptyDiv, tnode2);
<a name="line471"></a>  goog.dom.appendChild(emptyDiv, tnode3);
<a name="line472"></a>
<a name="line473"></a>  var range = goog.dom.Range.createFromNodes(emptyDiv, 1, emptyDiv, 2);
<a name="line474"></a>
<a name="line475"></a>  // Cannot use document.body.normalize() as it fails to normalize the div
<a name="line476"></a>  // (in IE) if it has nothing but empty text nodes.
<a name="line477"></a>  var newRange = goog.editor.range.rangePreservingNormalize(emptyDiv, range);
<a name="line478"></a>
<a name="line479"></a>  if (goog.userAgent.GECKO &amp;&amp;
<a name="line480"></a>      goog.string.compareVersions(goog.userAgent.VERSION, &#39;1.9&#39;) == -1) {
<a name="line481"></a>    // In FF2, node.normalize() leaves an empty textNode in the div, unlike
<a name="line482"></a>    // other browsers where the div is left with no children.
<a name="line483"></a>    goog.testing.dom.assertRangeEquals(
<a name="line484"></a>        emptyDiv.firstChild, 0, emptyDiv.firstChild, 0, newRange);
<a name="line485"></a>  } else {
<a name="line486"></a>    goog.testing.dom.assertRangeEquals(emptyDiv, 0, emptyDiv, 0, newRange);
<a name="line487"></a>  }
<a name="line488"></a>}
<a name="line489"></a>
<a name="line490"></a>function testRangeCreatedInVeryFragmentedDom() {
<a name="line491"></a>  var def = $(&#39;def&#39;);
<a name="line492"></a>  var defText = def.firstChild;
<a name="line493"></a>  var jkl = $(&#39;jkl&#39;);
<a name="line494"></a>  var jklText = jkl.firstChild;
<a name="line495"></a>
<a name="line496"></a>  // Fragment the DOM a bunch.
<a name="line497"></a>  fragmentText(defText);
<a name="line498"></a>  fragmentText(jklText);
<a name="line499"></a>
<a name="line500"></a>  // Notice that there are two empty text nodes at the beginning of each
<a name="line501"></a>  // fragmented node.
<a name="line502"></a>  var range = goog.dom.Range.createFromNodes(def, 3, jkl, 4);
<a name="line503"></a>
<a name="line504"></a>  var newRange = normalizeBody(range);
<a name="line505"></a>
<a name="line506"></a>  // our old text nodes may not be valid anymore. find new ones.
<a name="line507"></a>  defText = $(&#39;def&#39;).firstChild;
<a name="line508"></a>  jklText = $(&#39;jkl&#39;).firstChild;
<a name="line509"></a>  goog.testing.dom.assertRangeEquals(defText, 1, jklText, 2, newRange);
<a name="line510"></a>}
<a name="line511"></a>
<a name="line512"></a>function testNormalizeInFragmentedDomWithPreviousSiblings() {
<a name="line513"></a>  var ghiText = $(&#39;def&#39;).nextSibling;
<a name="line514"></a>  var mnoText = $(&#39;jkl&#39;).nextSibling;
<a name="line515"></a>  var range = goog.dom.Range.createFromNodes(ghiText, 1, mnoText, 2);
<a name="line516"></a>
<a name="line517"></a>  // Fragment the DOM a bunch.
<a name="line518"></a>  fragmentText($(&#39;def&#39;).previousSibling); // fragment abc
<a name="line519"></a>  fragmentText(ghiText);
<a name="line520"></a>  fragmentText(mnoText);
<a name="line521"></a>
<a name="line522"></a>  var newRange = normalizeBody(range);
<a name="line523"></a>
<a name="line524"></a>  // our old text nodes may not be valid anymore. find new ones.
<a name="line525"></a>  ghiText = $(&#39;def&#39;).nextSibling;
<a name="line526"></a>  mnoText = $(&#39;jkl&#39;).nextSibling;
<a name="line527"></a>
<a name="line528"></a>  goog.testing.dom.assertRangeEquals(ghiText, 1, mnoText, 2, newRange);
<a name="line529"></a>}
<a name="line530"></a>
<a name="line531"></a>function testRangeCreatedInFragmentedDomWithPreviousSiblings() {
<a name="line532"></a>  var def = $(&#39;def&#39;);
<a name="line533"></a>  var ghiText = $(&#39;def&#39;).nextSibling;
<a name="line534"></a>  var jkl = $(&#39;jkl&#39;);
<a name="line535"></a>  var mnoText = $(&#39;jkl&#39;).nextSibling;
<a name="line536"></a>
<a name="line537"></a>  // Fragment the DOM a bunch.
<a name="line538"></a>  fragmentText($(&#39;def&#39;).previousSibling); // fragment abc
<a name="line539"></a>  fragmentText(ghiText);
<a name="line540"></a>  fragmentText(mnoText);
<a name="line541"></a>
<a name="line542"></a>  // Notice that there are two empty text nodes at the beginning of each
<a name="line543"></a>  // fragmented node.
<a name="line544"></a>  var root = $(&#39;parentNode&#39;);
<a name="line545"></a>  var range = goog.dom.Range.createFromNodes(root, 9, root, 16);
<a name="line546"></a>
<a name="line547"></a>  var newRange = normalizeBody(range);
<a name="line548"></a>
<a name="line549"></a>  // our old text nodes may not be valid anymore. find new ones.
<a name="line550"></a>  ghiText = $(&#39;def&#39;).nextSibling;
<a name="line551"></a>  mnoText = $(&#39;jkl&#39;).nextSibling;
<a name="line552"></a>  goog.testing.dom.assertRangeEquals(ghiText, 1, mnoText, 2, newRange);
<a name="line553"></a>}
<a name="line554"></a>
<a name="line555"></a>
<a name="line556"></a>/**
<a name="line557"></a> * Branched from the tests for goog.dom.SavedCaretRange.
<a name="line558"></a> */
<a name="line559"></a>function testSavedCaretRange() {
<a name="line560"></a>  var def = $(&#39;def-1&#39;);
<a name="line561"></a>  var jkl = $(&#39;jkl-1&#39;);
<a name="line562"></a>
<a name="line563"></a>  var range = goog.dom.Range.createFromNodes(
<a name="line564"></a>      def.firstChild, 1, jkl.firstChild, 2);
<a name="line565"></a>  range.select();
<a name="line566"></a>
<a name="line567"></a>  var saved = goog.editor.range.saveUsingNormalizedCarets(range);
<a name="line568"></a>  assertHTMLEquals(
<a name="line569"></a>      &#39;d&lt;span id=&quot;&#39; + saved.startCaretId_ + &#39;&quot;&gt;&lt;/span&gt;ef&#39;, def.innerHTML);
<a name="line570"></a>  assertHTMLEquals(
<a name="line571"></a>      &#39;jk&lt;span id=&quot;&#39; + saved.endCaretId_ + &#39;&quot;&gt;&lt;/span&gt;l&#39;, jkl.innerHTML);
<a name="line572"></a>
<a name="line573"></a>  clearSelectionAndRestoreSaved(saved);
<a name="line574"></a>
<a name="line575"></a>  var selection = goog.dom.Range.createFromWindow(window);
<a name="line576"></a>  def = $(&#39;def-1&#39;);
<a name="line577"></a>  jkl = $(&#39;jkl-1&#39;);
<a name="line578"></a>  assertHTMLEquals(&#39;def&#39;, def.innerHTML);
<a name="line579"></a>  assertHTMLEquals(&#39;jkl&#39;, jkl.innerHTML);
<a name="line580"></a>
<a name="line581"></a>  // Check that everything was normalized ok.
<a name="line582"></a>  assertEquals(1, def.childNodes.length);
<a name="line583"></a>  assertEquals(1, jkl.childNodes.length);
<a name="line584"></a>  goog.testing.dom.assertRangeEquals(
<a name="line585"></a>      def.firstChild, 1, jkl.firstChild, 2, selection);
<a name="line586"></a>}
<a name="line587"></a>
<a name="line588"></a>function testRangePreservingNormalize() {
<a name="line589"></a>  var parent = $(&#39;normalizeTest-4&#39;);
<a name="line590"></a>  var def = $(&#39;def-4&#39;);
<a name="line591"></a>  var jkl = $(&#39;jkl-4&#39;);
<a name="line592"></a>  fragmentText(def.firstChild);
<a name="line593"></a>  fragmentText(jkl.firstChild);
<a name="line594"></a>
<a name="line595"></a>  var range = goog.dom.Range.createFromNodes(def, 3, jkl, 4);
<a name="line596"></a>  var oldRangeDescription = goog.testing.dom.exposeRange(range);
<a name="line597"></a>  range = goog.editor.range.rangePreservingNormalize(parent, range);
<a name="line598"></a>
<a name="line599"></a>  // Check that everything was normalized ok.
<a name="line600"></a>  assertEquals(&#39;def should have 1 child; range is &#39; +
<a name="line601"></a>      goog.testing.dom.exposeRange(range) +
<a name="line602"></a>      &#39;, range was &#39; + oldRangeDescription,
<a name="line603"></a>      1, def.childNodes.length);
<a name="line604"></a>  assertEquals(&#39;jkl should have 1 child; range is &#39; +
<a name="line605"></a>      goog.testing.dom.exposeRange(range) +
<a name="line606"></a>      &#39;, range was &#39; + oldRangeDescription,
<a name="line607"></a>      1, jkl.childNodes.length);
<a name="line608"></a>  goog.testing.dom.assertRangeEquals(def.firstChild, 1, jkl.firstChild, 2,
<a name="line609"></a>                                     range);
<a name="line610"></a>}
<a name="line611"></a>
<a name="line612"></a>function testRangePreservingNormalizeWhereEndNodePreviousSiblingIsSplit() {
<a name="line613"></a>  var parent = $(&#39;normalizeTest-with-br&#39;);
<a name="line614"></a>  var br = parent.childNodes[1];
<a name="line615"></a>  fragmentText(parent.firstChild);
<a name="line616"></a>
<a name="line617"></a>  var range = goog.dom.Range.createFromNodes(parent, 3, br, 0);
<a name="line618"></a>  range = goog.editor.range.rangePreservingNormalize(parent, range);
<a name="line619"></a>
<a name="line620"></a>  // Code used to throw an error here.
<a name="line621"></a>
<a name="line622"></a>  assertEquals(&#39;parent should have 3 children&#39;, 3, parent.childNodes.length);
<a name="line623"></a>  goog.testing.dom.assertRangeEquals(parent.firstChild, 1, parent, 1, range);
<a name="line624"></a>}
<a name="line625"></a>
<a name="line626"></a>function testRangePreservingNormalizeWhereStartNodePreviousSiblingIsSplit() {
<a name="line627"></a>  var parent = $(&#39;normalizeTest-with-br&#39;);
<a name="line628"></a>  var br = parent.childNodes[1];
<a name="line629"></a>  fragmentText(parent.firstChild);
<a name="line630"></a>  fragmentText(parent.lastChild);
<a name="line631"></a>
<a name="line632"></a>  var range = goog.dom.Range.createFromNodes(br, 0, parent, 9);
<a name="line633"></a>  range = goog.editor.range.rangePreservingNormalize(parent, range);
<a name="line634"></a>
<a name="line635"></a>  // Code used to throw an error here.
<a name="line636"></a>
<a name="line637"></a>  assertEquals(&#39;parent should have 3 children&#39;, 3, parent.childNodes.length);
<a name="line638"></a>  goog.testing.dom.assertRangeEquals(parent, 1, parent.lastChild, 1, range);
<a name="line639"></a>}
<a name="line640"></a>
<a name="line641"></a>function testSelectionPreservingNormalize1() {
<a name="line642"></a>  var parent = $(&#39;normalizeTest-2&#39;);
<a name="line643"></a>  var def = $(&#39;def-2&#39;);
<a name="line644"></a>  var jkl = $(&#39;jkl-2&#39;);
<a name="line645"></a>  fragmentText(def.firstChild);
<a name="line646"></a>  fragmentText(jkl.firstChild);
<a name="line647"></a>
<a name="line648"></a>  goog.dom.Range.createFromNodes(def, 3, jkl, 4).select();
<a name="line649"></a>  assertFalse(goog.dom.Range.createFromWindow(window).isReversed());
<a name="line650"></a>
<a name="line651"></a>  var oldRangeDescription = goog.testing.dom.exposeRange(
<a name="line652"></a>      goog.dom.Range.createFromWindow(window));
<a name="line653"></a>  goog.editor.range.selectionPreservingNormalize(parent);
<a name="line654"></a>
<a name="line655"></a>  // Check that everything was normalized ok.
<a name="line656"></a>  var range = goog.dom.Range.createFromWindow(window);
<a name="line657"></a>  assertFalse(range.isReversed());
<a name="line658"></a>
<a name="line659"></a>  assertEquals(&#39;def should have 1 child; range is &#39; +
<a name="line660"></a>      goog.testing.dom.exposeRange(range) +
<a name="line661"></a>      &#39;, range was &#39; + oldRangeDescription,
<a name="line662"></a>      1, def.childNodes.length);
<a name="line663"></a>  assertEquals(&#39;jkl should have 1 child; range is &#39; +
<a name="line664"></a>      goog.testing.dom.exposeRange(range) +
<a name="line665"></a>      &#39;, range was &#39; + oldRangeDescription,
<a name="line666"></a>      1, jkl.childNodes.length);
<a name="line667"></a>  goog.testing.dom.assertRangeEquals(def.firstChild, 1, jkl.firstChild, 2,
<a name="line668"></a>      range);
<a name="line669"></a>}
<a name="line670"></a>
<a name="line671"></a>
<a name="line672"></a>/**
<a name="line673"></a> * Make sure that selectionPreservingNormalize doesn&#39;t explode with no
<a name="line674"></a> * selection in the document.
<a name="line675"></a> */
<a name="line676"></a>function testSelectionPreservingNormalize2() {
<a name="line677"></a>  var parent = $(&#39;normalizeTest-3&#39;);
<a name="line678"></a>  var def = $(&#39;def-3&#39;);
<a name="line679"></a>  var jkl = $(&#39;jkl-3&#39;);
<a name="line680"></a>  def.firstChild.splitText(1);
<a name="line681"></a>  jkl.firstChild.splitText(2);
<a name="line682"></a>
<a name="line683"></a>  goog.dom.Range.clearSelection(window);
<a name="line684"></a>  goog.editor.range.selectionPreservingNormalize(parent);
<a name="line685"></a>
<a name="line686"></a>  // Check that everything was normalized ok.
<a name="line687"></a>  assertEquals(1, def.childNodes.length);
<a name="line688"></a>  assertEquals(1, jkl.childNodes.length);
<a name="line689"></a>  assertFalse(goog.dom.Range.hasSelection(window));
<a name="line690"></a>}
<a name="line691"></a>
<a name="line692"></a>function testSelectionPreservingNormalize3() {
<a name="line693"></a>  if (goog.userAgent.IE) {
<a name="line694"></a>    return;
<a name="line695"></a>  }
<a name="line696"></a>  var parent = $(&#39;normalizeTest-2&#39;);
<a name="line697"></a>  var def = $(&#39;def-2&#39;);
<a name="line698"></a>  var jkl = $(&#39;jkl-2&#39;);
<a name="line699"></a>  fragmentText(def.firstChild);
<a name="line700"></a>  fragmentText(jkl.firstChild);
<a name="line701"></a>
<a name="line702"></a>  goog.dom.Range.createFromNodes(jkl, 4, def, 3).select();
<a name="line703"></a>  assertTrue(goog.dom.Range.createFromWindow(window).isReversed());
<a name="line704"></a>
<a name="line705"></a>  var oldRangeDescription = goog.testing.dom.exposeRange(
<a name="line706"></a>      goog.dom.Range.createFromWindow(window));
<a name="line707"></a>  goog.editor.range.selectionPreservingNormalize(parent);
<a name="line708"></a>
<a name="line709"></a>  // Check that everything was normalized ok.
<a name="line710"></a>  var range = goog.dom.Range.createFromWindow(window);
<a name="line711"></a>  assertTrue(range.isReversed());
<a name="line712"></a>
<a name="line713"></a>  assertEquals(&#39;def should have 1 child; range is &#39; +
<a name="line714"></a>      goog.testing.dom.exposeRange(range) +
<a name="line715"></a>      &#39;, range was &#39; + oldRangeDescription,
<a name="line716"></a>      1, def.childNodes.length);
<a name="line717"></a>  assertEquals(&#39;jkl should have 1 child; range is &#39; +
<a name="line718"></a>      goog.testing.dom.exposeRange(range) +
<a name="line719"></a>      &#39;, range was &#39; + oldRangeDescription,
<a name="line720"></a>      1, jkl.childNodes.length);
<a name="line721"></a>  goog.testing.dom.assertRangeEquals(def.firstChild, 1, jkl.firstChild, 2,
<a name="line722"></a>      range);
<a name="line723"></a>}
<a name="line724"></a>
<a name="line725"></a>function testSelectionPreservingNormalizeAfterPlaceCursorNextTo() {
<a name="line726"></a>  var parent = $(&#39;normalizeTest-with-div&#39;);
<a name="line727"></a>  goog.editor.range.placeCursorNextTo(parent.firstChild);
<a name="line728"></a>  goog.editor.range.selectionPreservingNormalize(parent);
<a name="line729"></a>
<a name="line730"></a>  // Code used to throw an exception here.
<a name="line731"></a>}
<a name="line732"></a>
<a name="line733"></a>
<a name="line734"></a>/** Normalize the body and return the normalized range. */
<a name="line735"></a>function normalizeBody(range) {
<a name="line736"></a>  var rangeFactory = goog.editor.range.normalize(range);
<a name="line737"></a>  document.body.normalize();
<a name="line738"></a>  return rangeFactory();
<a name="line739"></a>}
<a name="line740"></a>
<a name="line741"></a>
<a name="line742"></a>/** Break a text node up into lots of little fragments. */
<a name="line743"></a>function fragmentText(text) {
<a name="line744"></a>  // NOTE(nicksantos): For some reason, splitText makes IE deeply
<a name="line745"></a>  // unhappy to the point where normalize and other normal DOM operations
<a name="line746"></a>  // start failing. It&#39;s a useful test for Firefox though, because different
<a name="line747"></a>  // versions of FireFox handle empty text nodes differently.
<a name="line748"></a>  // See goog.editor.BrowserFeature.
<a name="line749"></a>  if (goog.userAgent.IE) {
<a name="line750"></a>    manualSplitText(text, 2);
<a name="line751"></a>    manualSplitText(text, 1);
<a name="line752"></a>    manualSplitText(text, 0);
<a name="line753"></a>    manualSplitText(text, 0);
<a name="line754"></a>  } else {
<a name="line755"></a>    text.splitText(2);
<a name="line756"></a>    text.splitText(1);
<a name="line757"></a>
<a name="line758"></a>    text.splitText(0);
<a name="line759"></a>    text.splitText(0);
<a name="line760"></a>  }
<a name="line761"></a>}
<a name="line762"></a>
<a name="line763"></a>
<a name="line764"></a>/**
<a name="line765"></a> * Clear the selection by re-parsing the DOM. Then restore the saved
<a name="line766"></a> * selection.
<a name="line767"></a> * @param {goog.dom.SavedRange} saved The saved range.
<a name="line768"></a> */
<a name="line769"></a>function clearSelectionAndRestoreSaved(saved) {
<a name="line770"></a>  goog.dom.Range.clearSelection(window);
<a name="line771"></a>  assertFalse(goog.dom.Range.hasSelection(window));
<a name="line772"></a>  saved.restore();
<a name="line773"></a>  assertTrue(goog.dom.Range.hasSelection(window));
<a name="line774"></a>}
<a name="line775"></a>
<a name="line776"></a>function manualSplitText(node, pos) {
<a name="line777"></a>  var newNodeString = node.nodeValue.substr(pos);
<a name="line778"></a>  node.nodeValue = node.nodeValue.substr(0, pos);
<a name="line779"></a>  goog.dom.insertSiblingAfter(document.createTextNode(newNodeString), node);
<a name="line780"></a>}
<a name="line781"></a>
<a name="line782"></a>function testSelectNodeStartSimple() {
<a name="line783"></a>  var div = $(&#39;parentNode&#39;);
<a name="line784"></a>  div.innerHTML = &#39;&lt;p&gt;Cursor should go in here&lt;/p&gt;&#39;;
<a name="line785"></a>
<a name="line786"></a>  goog.editor.range.selectNodeStart(div);
<a name="line787"></a>  var range = goog.dom.Range.createFromWindow(window);
<a name="line788"></a>  // Gotta love browsers and their inconsistencies with selection
<a name="line789"></a>  // representations.  What we are trying to achieve is that when we type
<a name="line790"></a>  // the text will go into the P node.  In Gecko, the selection is at the start
<a name="line791"></a>  // of the text node, as you&#39;d expect, but in pre-530 Webkit, it has been
<a name="line792"></a>  // normalized to the visible position of P:0.
<a name="line793"></a>  if (goog.userAgent.GECKO || goog.userAgent.IE ||
<a name="line794"></a>      (goog.userAgent.WEBKIT &amp;&amp; goog.userAgent.isVersionOrHigher(&#39;530&#39;))) {
<a name="line795"></a>    goog.testing.dom.assertRangeEquals(div.firstChild.firstChild, 0,
<a name="line796"></a>        div.firstChild.firstChild, 0, range);
<a name="line797"></a>  } else {
<a name="line798"></a>    goog.testing.dom.assertRangeEquals(div.firstChild, 0,
<a name="line799"></a>        div.firstChild, 0, range);
<a name="line800"></a>  }
<a name="line801"></a>}
<a name="line802"></a>
<a name="line803"></a>function testSelectNodeStartBr() {
<a name="line804"></a>  var div = $(&#39;parentNode&#39;);
<a name="line805"></a>  div.innerHTML = &#39;&lt;p&gt;&lt;br&gt;Cursor should go in here&lt;/p&gt;&#39;;
<a name="line806"></a>
<a name="line807"></a>  goog.editor.range.selectNodeStart(div);
<a name="line808"></a>  var range = goog.dom.Range.createFromWindow(window);
<a name="line809"></a>  // We have to skip the BR since Gecko can&#39;t render a cursor at a BR.
<a name="line810"></a>  goog.testing.dom.assertRangeEquals(div.firstChild, 0,
<a name="line811"></a>      div.firstChild, 0, range);
<a name="line812"></a>}
<a name="line813"></a>
<a name="line814"></a>function testIsEditable() {
<a name="line815"></a>  var containerElement = document.getElementById(&#39;editableTest&#39;);
<a name="line816"></a>  // Find editable container element&#39;s index.
<a name="line817"></a>  var containerIndex = 0;
<a name="line818"></a>  var currentSibling = containerElement;
<a name="line819"></a>  while (currentSibling = currentSibling.previousSibling) {
<a name="line820"></a>    containerIndex++;
<a name="line821"></a>  }
<a name="line822"></a>
<a name="line823"></a>  var editableContainer = goog.dom.Range.createFromNodes(
<a name="line824"></a>      containerElement.parentNode, containerIndex,
<a name="line825"></a>      containerElement.parentNode, containerIndex + 1);
<a name="line826"></a>  assertFalse(&#39;Range containing container element not considered editable&#39;,
<a name="line827"></a>      goog.editor.range.isEditable(editableContainer));
<a name="line828"></a>
<a name="line829"></a>  var allEditableChildren = goog.dom.Range.createFromNodes(
<a name="line830"></a>      containerElement, 0, containerElement,
<a name="line831"></a>      containerElement.childNodes.length);
<a name="line832"></a>  assertTrue(&#39;Range of all of container element children considered editable&#39;,
<a name="line833"></a>      goog.editor.range.isEditable(allEditableChildren));
<a name="line834"></a>
<a name="line835"></a>  var someEditableChildren = goog.dom.Range.createFromNodes(
<a name="line836"></a>      containerElement, 2, containerElement, 6);
<a name="line837"></a>  assertTrue(&#39;Range of some container element children considered editable&#39;,
<a name="line838"></a>      goog.editor.range.isEditable(someEditableChildren));
<a name="line839"></a>
<a name="line840"></a>
<a name="line841"></a>  var mixedEditableNonEditable = goog.dom.Range.createFromNodes(
<a name="line842"></a>      containerElement.previousSibling, 0, containerElement, 2);
<a name="line843"></a>  assertFalse(&#39;Range overlapping some content not considered editable&#39;,
<a name="line844"></a>      goog.editor.range.isEditable(mixedEditableNonEditable));
<a name="line845"></a>}
<a name="line846"></a>
<a name="line847"></a>function testIntersectsTag() {
<a name="line848"></a>  var root = $(&#39;root&#39;);
<a name="line849"></a>  root.innerHTML =
<a name="line850"></a>      &#39;&lt;b&gt;Bold&lt;/b&gt;&lt;p&gt;&lt;span&gt;&lt;code&gt;x&lt;/code&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;y&lt;/p&gt;&lt;i&gt;Italic&lt;/i&gt;&#39;;
<a name="line851"></a>
<a name="line852"></a>  // Select the whole thing.
<a name="line853"></a>  var range = goog.dom.Range.createFromNodeContents(root);
<a name="line854"></a>  assertTrue(goog.editor.range.intersectsTag(range, goog.dom.TagName.DIV));
<a name="line855"></a>  assertTrue(goog.editor.range.intersectsTag(range, goog.dom.TagName.B));
<a name="line856"></a>  assertTrue(goog.editor.range.intersectsTag(range, goog.dom.TagName.I));
<a name="line857"></a>  assertTrue(goog.editor.range.intersectsTag(range, goog.dom.TagName.CODE));
<a name="line858"></a>  assertFalse(goog.editor.range.intersectsTag(range, goog.dom.TagName.U));
<a name="line859"></a>
<a name="line860"></a>  // Just select italic.
<a name="line861"></a>  range = goog.dom.Range.createFromNodes(root, 3, root, 4);
<a name="line862"></a>  assertTrue(goog.editor.range.intersectsTag(range, goog.dom.TagName.DIV));
<a name="line863"></a>  assertFalse(goog.editor.range.intersectsTag(range, goog.dom.TagName.B));
<a name="line864"></a>  assertTrue(goog.editor.range.intersectsTag(range, goog.dom.TagName.I));
<a name="line865"></a>  assertFalse(goog.editor.range.intersectsTag(range, goog.dom.TagName.CODE));
<a name="line866"></a>  assertFalse(goog.editor.range.intersectsTag(range, goog.dom.TagName.U));
<a name="line867"></a>
<a name="line868"></a>  // Select &quot;ld x y&quot;.
<a name="line869"></a>  range = goog.dom.Range.createFromNodes(root.firstChild.firstChild, 2,
<a name="line870"></a>      root.childNodes[2], 1);
<a name="line871"></a>  assertTrue(goog.editor.range.intersectsTag(range, goog.dom.TagName.DIV));
<a name="line872"></a>  assertTrue(goog.editor.range.intersectsTag(range, goog.dom.TagName.B));
<a name="line873"></a>  assertFalse(goog.editor.range.intersectsTag(range, goog.dom.TagName.I));
<a name="line874"></a>  assertTrue(goog.editor.range.intersectsTag(range, goog.dom.TagName.CODE));
<a name="line875"></a>  assertFalse(goog.editor.range.intersectsTag(range, goog.dom.TagName.U));
<a name="line876"></a>
<a name="line877"></a>  // Select ol.
<a name="line878"></a>  range = goog.dom.Range.createFromNodes(root.firstChild.firstChild, 1,
<a name="line879"></a>      root.firstChild.firstChild, 3);
<a name="line880"></a>  assertTrue(goog.editor.range.intersectsTag(range, goog.dom.TagName.DIV));
<a name="line881"></a>  assertTrue(goog.editor.range.intersectsTag(range, goog.dom.TagName.B));
<a name="line882"></a>  assertFalse(goog.editor.range.intersectsTag(range, goog.dom.TagName.I));
<a name="line883"></a>  assertFalse(goog.editor.range.intersectsTag(range, goog.dom.TagName.CODE));
<a name="line884"></a>  assertFalse(goog.editor.range.intersectsTag(range, goog.dom.TagName.U));
<a name="line885"></a>}
<a name="line886"></a>
<a name="line887"></a>function testNormalizeNode() {
<a name="line888"></a>  var div = goog.dom.createDom(&#39;DIV&#39;, null, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;);
<a name="line889"></a>  assertEquals(3, div.childNodes.length);
<a name="line890"></a>  goog.editor.range.normalizeNode(div);
<a name="line891"></a>  assertEquals(1, div.childNodes.length);
<a name="line892"></a>  assertEquals(&#39;abc&#39;, div.firstChild.nodeValue);
<a name="line893"></a>
<a name="line894"></a>  div = goog.dom.createDom(&#39;DIV&#39;, null,
<a name="line895"></a>      goog.dom.createDom(&#39;SPAN&#39;, null, &#39;1&#39;, &#39;2&#39;),
<a name="line896"></a>      goog.dom.createTextNode(&#39;&#39;),
<a name="line897"></a>      goog.dom.createDom(&#39;BR&#39;),
<a name="line898"></a>      &#39;b&#39;,
<a name="line899"></a>      &#39;c&#39;);
<a name="line900"></a>  assertEquals(5, div.childNodes.length);
<a name="line901"></a>  assertEquals(2, div.firstChild.childNodes.length);
<a name="line902"></a>  goog.editor.range.normalizeNode(div);
<a name="line903"></a>  if (goog.userAgent.GECKO &amp;&amp; !goog.userAgent.isVersionOrHigher(1.9) ||
<a name="line904"></a>      goog.userAgent.WEBKIT &amp;&amp; !goog.userAgent.isVersionOrHigher(526)) {
<a name="line905"></a>    // Old Gecko and Webkit versions don&#39;t delete the empty node.
<a name="line906"></a>    assertEquals(4, div.childNodes.length);
<a name="line907"></a>  } else {
<a name="line908"></a>    assertEquals(3, div.childNodes.length);
<a name="line909"></a>  }
<a name="line910"></a>  assertEquals(1, div.firstChild.childNodes.length);
<a name="line911"></a>  assertEquals(&#39;12&#39;, div.firstChild.firstChild.nodeValue);
<a name="line912"></a>  assertEquals(&#39;bc&#39;, div.lastChild.nodeValue);
<a name="line913"></a>  assertEquals(&#39;BR&#39;, div.lastChild.previousSibling.tagName);
<a name="line914"></a>}
<a name="line915"></a>
<a name="line916"></a>function testDeepestPoint() {
<a name="line917"></a>  var parent = $(&#39;parentNode&#39;);
<a name="line918"></a>  var def = $(&#39;def&#39;);
<a name="line919"></a>
<a name="line920"></a>  assertEquals(def, parent.childNodes[1]);
<a name="line921"></a>
<a name="line922"></a>  var deepestPoint = goog.editor.range.Point.createDeepestPoint;
<a name="line923"></a>
<a name="line924"></a>  var defStartLeft = deepestPoint(parent, 1, true);
<a name="line925"></a>  assertPointEquals(def.previousSibling, def.previousSibling.nodeValue.length,
<a name="line926"></a>      defStartLeft);
<a name="line927"></a>
<a name="line928"></a>  var defStartRight = deepestPoint(parent, 1, false);
<a name="line929"></a>  assertPointEquals(def.firstChild, 0, defStartRight);
<a name="line930"></a>
<a name="line931"></a>  var defEndLeft = deepestPoint(parent, 2, true);
<a name="line932"></a>  assertPointEquals(def.firstChild, def.firstChild.nodeValue.length,
<a name="line933"></a>      defEndLeft);
<a name="line934"></a>
<a name="line935"></a>  var defEndRight = deepestPoint(parent, 2, false);
<a name="line936"></a>  assertPointEquals(def.nextSibling, 0, defEndRight);
<a name="line937"></a>}
<a name="line938"></a>
<a name="line939"></a>function assertPointEquals(node, offset, actualPoint) {
<a name="line940"></a>  assertEquals(&#39;Point has wrong node&#39;, node, actualPoint.node);
<a name="line941"></a>  assertEquals(&#39;Point has wrong offset&#39;, offset, actualPoint.offset);
<a name="line942"></a>}
</pre>


</body>
</html>
